<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

	<!-- get translations -->
	<xsl:variable name="i18n" select="document($Layout/Server/i18n)/*"/>

	<xsl:template name="i18n">
		<xsl:param name="Group" select="''"/>
		<xsl:param name="Key" select="''"/>
		<xsl:variable name="val" select="$i18n/*[ name() = $Group ]/*[ @ID = $Key ]"/>
		<xsl:choose>
			<!-- search for the exact language string -->
			<xsl:when test="$val/*[ lang($Language) ]">
				<xsl:value-of select="$val/*[ lang($Language) ][1]"/>
			</xsl:when>
			<!-- search for the primary language (2-character string before the hyphen) -->
			<xsl:when test="$val/*[ lang($PrimaryLanguage) ]">
				<xsl:value-of select="$val/*[ lang($PrimaryLanguage) ][1]"/>
			</xsl:when>
			<!-- if we have a value without the lang attribute, use it -->
			<xsl:when test="$val">
				<xsl:value-of select="$val"/>
			</xsl:when>
			<!-- otherwise, show an error -->
			<xsl:otherwise>
				<xsl:message terminate="no">
					<xsl:text>Missing xml:lang item(</xsl:text>
					<xsl:value-of select="concat($Group,'/',$Key,'/',$Language)"/>
					<xsl:text>)</xsl:text>
				</xsl:message>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="reverse-i18n">
		<xsl:param name="Group" select="''"/>
		<xsl:param name="Label" select="''"/>
		<xsl:value-of select="$i18n/*[ name()=$Group ]/*[ Name=$Label ]/@ID"/>
	</xsl:template>

</xsl:stylesheet>
